package com.stormragetech.sylwanas.common;

import com.stormragetech.sylwanas.process.MessageConsumer;
import com.stormragetech.sylwanas.process.MessageSender;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.UUID;
import java.util.concurrent.atomic.LongAdder;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * description: //TODO
 *
 * @author: Xue Bin
 * @version: 1.0.0-SNAPSHOT
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class MessageWrapper {

    private String id;
    private LongAdder count;
    private int confirmCode; // 0: init; 1: success; -1: fail
    private int returnCode;  // 0: init; -1: fail
    private Lock lock;
    private long time; // send time
    Object message;
    MessageSender sender;
    MessageConsumer consumer;

    public MessageWrapper(Object message, MessageSender sender) {
        this.id = UUID.randomUUID().toString().trim();
//        this.id = UUID.randomUUID().toString().replace("-", "");
        this.count = new LongAdder();
        this.message = message;
        this.sender = sender;
        this.lock = new ReentrantLock();
        lock();
    }

    public MessageWrapper(String id, Object message, MessageConsumer consumer) {
        this.id = id;
        this.count = new LongAdder();
        this.message = message;
        this.consumer = consumer;
    }

    public void lock() {
        lock.lock();
    }

    public void unlock() {
        lock.unlock();
    }

    public int increase() {
        count.increment();
        return count.intValue();
    }

}
